package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysWhitePaperKnowledgeBase;
import com.ruoyi.system.service.ISysWhitePaperKnowledgeBaseService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysWhitePaperRequireMapper;
import com.ruoyi.system.domain.SysWhitePaperRequire;
import com.ruoyi.system.service.ISysWhitePaperRequireService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 白皮书编写要求Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-20
 */
@Service
public class SysWhitePaperRequireServiceImpl implements ISysWhitePaperRequireService
{
    @Autowired
    private SysWhitePaperRequireMapper sysWhitePaperRequireMapper;
    @Autowired
    private ISysWhitePaperKnowledgeBaseService sysWhitePaperKnowledgeBaseService;

    @Override
    public SysWhitePaperRequire getRequireAndInsdutryByProjectId(Long projectId) {
        return sysWhitePaperRequireMapper.getRequireAndInsdutryByProjectId(projectId);
    }

    @Override
    public SysWhitePaperRequire selectSysWhitePaperRequireByProjectId(Long projectId) {
        // 编写要求
        SysWhitePaperRequire whitePaperRequire = sysWhitePaperRequireMapper.selectSysWhitePaperRequireByProjectId(projectId);

        // 知识库列表
        SysWhitePaperKnowledgeBase whitePaperKnowledgeBase = new SysWhitePaperKnowledgeBase();
        whitePaperKnowledgeBase.setProjectId(projectId);
        List<SysWhitePaperKnowledgeBase> knowledgeBaseList = sysWhitePaperKnowledgeBaseService.selectSysWhitePaperKnowledgeBaseList(whitePaperKnowledgeBase);

        whitePaperRequire.setKnowledgeBaseList(knowledgeBaseList);
        return whitePaperRequire;
    }

    /**
     * 查询白皮书编写要求
     *
     * @param id 白皮书编写要求主键
     * @return 白皮书编写要求
     */
    @Override
    public SysWhitePaperRequire selectSysWhitePaperRequireById(Long id)
    {
        return sysWhitePaperRequireMapper.selectSysWhitePaperRequireById(id);
    }

    /**
     * 查询白皮书编写要求列表
     *
     * @param sysWhitePaperRequire 白皮书编写要求
     * @return 白皮书编写要求
     */
    @Override
    public List<SysWhitePaperRequire> selectSysWhitePaperRequireList(SysWhitePaperRequire sysWhitePaperRequire)
    {
        return sysWhitePaperRequireMapper.selectSysWhitePaperRequireList(sysWhitePaperRequire);
    }

    /**
     * 新增白皮书编写要求
     *
     * @param sysWhitePaperRequire 白皮书编写要求
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertSysWhitePaperRequire(SysWhitePaperRequire sysWhitePaperRequire)
    {
        Long projectId = sysWhitePaperRequire.getProjectId();
        // 1,根据项目ID删除编写要求
        sysWhitePaperRequireMapper.deleteSysWhitePaperRequireByProjectId(projectId);

        // 2,根据项目ID删除知识库
        sysWhitePaperKnowledgeBaseService.deleteSysWhitePaperKnowledgeBaseByProjectId(projectId);

        String loginUserName = SecurityUtils.getUsername();
        Date currentDate = DateUtils.getNowDate();
        // 3,新增编写要求
        sysWhitePaperRequire.setCreateBy(loginUserName);
        sysWhitePaperRequire.setCreateTime(currentDate);
        sysWhitePaperRequireMapper.insertSysWhitePaperRequire(sysWhitePaperRequire);

        // 4,新增知识库
        List<SysWhitePaperKnowledgeBase> knowledgeBaseList = sysWhitePaperRequire.getKnowledgeBaseList();
        if (CollectionUtils.isNotEmpty(knowledgeBaseList)) {
            knowledgeBaseList.forEach(knowledgeBase -> {
                knowledgeBase.setProjectId(projectId);
                knowledgeBase.setCreateBy(loginUserName);
                knowledgeBase.setCreateTime(currentDate);
            });

            sysWhitePaperKnowledgeBaseService.batchInsertSysWhitePaperKnowledgeBase(knowledgeBaseList);
        }
    }

    /**
     * 修改白皮书编写要求
     *
     * @param sysWhitePaperRequire 白皮书编写要求
     * @return 结果
     */
    @Override
    public int updateSysWhitePaperRequire(SysWhitePaperRequire sysWhitePaperRequire)
    {
        sysWhitePaperRequire.setUpdateTime(DateUtils.getNowDate());
        return sysWhitePaperRequireMapper.updateSysWhitePaperRequire(sysWhitePaperRequire);
    }

    /**
     * 批量删除白皮书编写要求
     *
     * @param ids 需要删除的白皮书编写要求主键
     * @return 结果
     */
    @Override
    public int deleteSysWhitePaperRequireByIds(Long[] ids)
    {
        return sysWhitePaperRequireMapper.deleteSysWhitePaperRequireByIds(ids);
    }

    /**
     * 删除白皮书编写要求信息
     *
     * @param id 白皮书编写要求主键
     * @return 结果
     */
    @Override
    public int deleteSysWhitePaperRequireById(Long id)
    {
        return sysWhitePaperRequireMapper.deleteSysWhitePaperRequireById(id);
    }
}
